package com.yang.service;

import com.yang.dao.CategoryDao;
import com.yang.entity.Category;
import com.yang.entity.Product;
import com.yang.dao.ProductDao;
import com.yang.pojo.HtmlProductVO;
import com.yang.pojo.categoryVO;
import com.yang.util.RespEntity;
import java.util.*;
public class ProductService {
    private final ProductDao productDao = new ProductDao();
    CategoryDao categoryDao = new CategoryDao();


    //多功查询
    public RespEntity queryBySql(String name,String sid) {
       StringBuffer sql=new StringBuffer("select * from product where 1=1 ");
       if (name!=null&&!"".equals(name)){
           sql.append(" and name like '%"+name+"%'");
       }
       if (sid != null && !"".equals(sid)){
           String num = " and categoryId = "+sid;
           sql.append(num);

       }
        System.out.println(sql.toString());
        List<Product> products = productDao.queryBySql(sql.toString());
        return new RespEntity(200,"多功能查询",products);
    }
    //单个查询//详情
    public RespEntity queryProductById(Integer id) {
        return new RespEntity(productDao.queryProductById(id));
    }

    //后台商品添加
    public RespEntity HtmlSaveProduct(Product obj) {
        int sum = productDao.saveProduct(obj);
        return new RespEntity(200,"添加",sum);
    }

    //删除
    public RespEntity deleteProductById(Integer id) {
        return new RespEntity(productDao.deleteProductById(id));
    }

    //后台商品信息修改
    public RespEntity HtmlUpdateProduct(Product pro) {
        StringBuffer sql=new StringBuffer("update product set ");
        if (pro.getName()!=null&&!"".equals(pro.getName())){
            sql.append("name='"+pro.getName()+"',");
        }
        if (pro.getPrice()!=null&&!"".equals(pro.getPrice())){
            sql.append("price='"+pro.getPrice()+"',");
        }
        if (pro.getDetail()!=null&&!"".equals(pro.getDetail())){
            sql.append("detail='"+pro.getDetail()+"',");
        }
        if (pro.getStock()!=null&&!"".equals(pro.getStock())){
            sql.append("stock='"+pro.getStock()+"',");
        }
        if (pro.getUrl()!=null&&!"".equals(pro.getUrl())){
            sql.append("url='"+pro.getUrl()+"',");
        }
        if (pro.getCategoryid()!=null&&!"".equals(pro.getCategoryid())){
            sql.append("categoryid ='"+pro.getCategoryid()+"'");
        }
        sql.append(" where id="+pro.getId());
        /*System.out.println("SQL语句"+sql.toString());*/
        int sum = productDao.updateBySql(sql.toString());
        return new RespEntity(200,"修改",sum);
    }
    //后台查询全部商品
    public RespEntity HtmlQueryProduct(){
        List<Product> products = productDao.queryProduct();
        List<Category> categories = categoryDao.queryCategory();
        //存放商品和分类的
        List<HtmlProductVO> list=new ArrayList<>();
        for (int sum = 0; sum < products.size(); sum++) {
            Product product = products.get(sum);
            Category category = categoryDao.queryCategoryByIdHTML(product.getCategoryid());
            Category category1 = categoryDao.queryCategoryByIdHTML(category.getParentid());
           /* System.out.println(product);
            System.out.println(category);
            System.out.println(category1);*/

            HtmlProductVO vo = new HtmlProductVO(product,category.getId(),category1.getName(),category.getName());
            list.add(vo);
        }
        return new RespEntity(200,"全部",list);
    }

    //后台查询全部分类
    public RespEntity queryProductCategory() {
        //得到全部的
        List<Category> categories = categoryDao.queryCategoryById(0);
        System.out.println(categories.toString());
        //装整理好的数据（父类和子类）
        List<categoryVO> categoryVO = new ArrayList<>();
        for (Category category : categories) {
            List<Category> sons = categoryDao.queryCategoryById(category.getId());
            //封装到VO里面
            categoryVO categoryVO1 = new categoryVO(category.getId(),category.getName(),category.getParentid(),sons);
            categoryVO.add(categoryVO1);
        }
        return new RespEntity(200,"开始查询",categoryVO);
    }
    //根据id查询当前商品，展示详情
    public RespEntity HtmlByIdProduct(Integer id) {
        //调用根据id查询商品的方法
        List<Category> categories = categoryDao.queryCategory();
        //VO
        Product product = productDao.queryProductById(id);
        Category category = categoryDao.queryCategoryByIdHTML(product.getCategoryid());
        Category category1 = categoryDao.queryCategoryByIdHTML(category.getParentid());
           /* System.out.println("单个查询商品"+product);
            System.out.println("单个查询商品查询id"+category);
            System.out.println("分类"+category1);*/

        HtmlProductVO vo = new HtmlProductVO(product,category.getId(),category1.getName(),category.getName());


        return  new RespEntity(200,"查询成功",vo);
    }

    //根据id查询当前商品，展示详情,分类表的分类
    public RespEntity HtmlByIdProductAndCategory(Integer id) {
        //调用根据id查询商品的方法
        //VO
        Product product = productDao.queryProductById(id);
        //查询全部分类/父
        List<categoryVO> categoryVO = new ArrayList<>();
        List<Category> parent = categoryDao.queryCategoryById(0);
        //循环父查询全部子
        for (Category category2 : parent) {
            List<Category> sons = categoryDao.queryCategoryById(category2.getId());
            categoryVO categoryvo = new categoryVO(category2.getId(),category2.getName(),sons);
            categoryVO.add(categoryvo);
           /* System.out.println("儿子的全部"+sons);*/
        }
        //创建一个map存
        Map map=new HashMap();
        map.put("products",product);
        map.put("category",categoryVO);
       /* System.out.println("单个查询商品"+product);
        System.out.println("父类全部"+parent);*/

        return  new RespEntity(200,"查询成功",map);
    }
    //模糊查询
    public RespEntity HtmlBySQL(String name,String sid) {
        StringBuffer sb=new StringBuffer("select * from product where 1=1 ");

        if (name!=null&&!"".equals(name)){
            sb.append(" and name like '%"+name+"%'");
        }
        if ( !"0".equals(sid)){
            String num = " and categoryId = "+sid;
            sb.append(num);

        }
        System.out.println(sb.toString());
        List<Product> products = productDao.queryBySql(sb.toString());
        System.out.println("商品："+products.toString());

        List<Category> categories = categoryDao.queryCategory();
        //存放商品和分类的
        List<HtmlProductVO> list=new ArrayList<>();
        for (int sum = 0; sum < products.size(); sum++) {
            Product product = products.get(sum);
            Category category = categoryDao.queryCategoryByIdHTML(product.getCategoryid());
            Category category1 = categoryDao.queryCategoryByIdHTML(category.getParentid());
            HtmlProductVO vo = new HtmlProductVO(product,category.getId(),category1.getName(),category.getName());
            list.add(vo);
        }
        return new RespEntity(200,"多功能查询",list);
    }
}
